<?php
/*
Plugin Name: 万能附件镜像存储
Plugin URI: https://www.beizigen.com/post/wordpress-universal-attachment-cloud-storage-plug-in-attachment-cdn/
Description: 支持七牛、又拍、腾讯COS、阿里OSS等具有镜像功能的云存储。
Version: 1.1.1
Author: 背字根
Author URI: https://www.beizigen.com
*/

require_once('functions.php');

//停用插件删除设置
function bzg_attachment_cdn_clean()
{
	bzg_attachment_cdn_delete_cron();
	update_option('upload_url_path', '');
	update_option('upload_path', '');
	delete_option('attachment_cdn_ids');
	delete_option('attachment_cdn_options');
}
register_deactivation_hook(__FILE__, 'bzg_attachment_cdn_clean');
register_uninstall_hook(__FILE__, 'bzg_attachment_cdn_clean');

function bzg_attachment_cdn_control()
{
	$message = '';
	if (wp_next_scheduled('attachment_cdn')) {
		$message = '上传任务正在执行，请等待任务结束或重启插件以强制终止任务！';
	}

	if (isset($_POST['action']) && '保存设置' == $_POST['action']) {
		if (!empty($_POST['uppath']) && 'wp-content/uploads' != $_POST['uppath']) {
			$upload_path = untrailingslashit(trim($_POST['uppath']));
			update_option('upload_path', $upload_path);
		} else {
			$upload_path = 'wp-content/uploads';
			update_option('upload_path', '');
		}

		$up_path = '';
		if (!empty($_POST['domain'])) {
			$up_path = $_POST['cdnscheme'] . '://' . trim($_POST['domain']) . '/' . $upload_path;
		}

		update_option('upload_url_path', $up_path);

		$data = array(
			'delfile' => $_POST['delfile'],
			'backupcdn' => trim($_POST['backupcdn']),
			'cdnscheme' => $_POST['cdnscheme'],
			'backupcdnscheme' => $_POST['backupcdnscheme'],
		);
		$data = serialize($data);
		update_option('attachment_cdn_options', $data);
		$message = '保存设置成功！';
	}

	if (isset($_POST['action']) && '初始化数据' == $_POST['action']) {
		if (get_option('upload_url_path')) {
			$message = '任务已开始执行...';
			$start = bzg_attachment_cdn_add_cron();
			if (!$start) {
				$message = '任务正在执行，请等待任务结束或重启插件以强制终止任务！';
			}
		} else {
			$message = '请先填写镜像域名！';
		}
	}

	if (!empty($message)) {
		echo '<div class="notice is-dismissible updated" style="padding: 1em;">' . $message . '</div>';
	}

	$parse_url = array();

	if ($upload_url_path = get_option('upload_url_path')) {
		$parse_url = parse_url($upload_url_path);
	}

	$options = get_option('attachment_cdn_options');
	$options = unserialize($options);

?>
	<div class="wrap">
		<h2>WordPress附件镜像存储</h2>
		<form method="post" action="">
			<table class="form-table">
				<tbody>
					<tr>
						<th scope="row">
							<label for="uppath">上传路径</label>
						</th>
						<td>
							<input id="uppath" class="regular-text code" type="text" value="<?php echo get_option('upload_path') ? esc_attr(get_option('upload_path')) : esc_attr('wp-content/uploads'); ?>" name="uppath">
							<p class="description">一般使用WordPress默认设置，即wp-content/uploads</p>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label for="domain">CDN域名</label>
						</th>
						<td>
							<select id="cdnscheme" name="cdnscheme">
								<option value="http">http://</option>
								<option value="https" <?php echo 'https' == $options['cdnscheme'] ? ' selected="selected"' : ''; ?>>https://</option>
							</select>
							<input id="domain" class="regular-text code" type="text" value="<?php echo !empty($parse_url['host']) ? $parse_url['host'] : ''; ?>" name="domain">
							<p class="description">七牛、又拍、腾讯COS等提供的域名或您在这些平台绑定的域名，例如：static.beizigen.com</p>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label for="backupcdn">备用CDN</label>
						</th>
						<td>
							<select id="backupcdnscheme" name="backupcdnscheme">
								<option value="http">http://</option>
								<option value="https" <?php echo 'https' == $options['backupcdnscheme'] ? ' selected="selected"' : ''; ?>>https://</option>
							</select>
							<input id="backupcdn" class="regular-text code" type="text" value="<?php echo !$options['backupcdn'] ? $options['backupcdn'] : ''; ?>" name="backupcdn">
							<p class="description">可不用填写，同CDN域名设置，上传的附件会同步到该空间，但不会在网站调用，用于附件备份</p>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label for="delfile">删除本地文件</label>
						</th>
						<td>
							<select id="delfile" name="delfile">
								<option value="no">否</option>
								<option value="yes" <?php echo 'yes' == $options['delfile'] ? ' selected="selected"' : ''; ?>>是</option>
							</select>
							<p class="description">不推荐，多一份备份多一份安全</p>
						</td>
					</tr>
				</tbody>
			</table>
			<p><input class="button-primary" type="submit" name="action" value="保存设置"></p>
			<p class="description">如果是第一次安装使用，可以使用初始化功能将现有图片全部同步到云存储</p>
			<p class="description">在点击初始化数据按钮后，为了确保任务顺利执行，请点击<a href="<?php bloginfo('url'); ?>/wp-cron.php" target="_blank">访问WordPress定时任务加载页面</a></p>
			<p><input class="button" type="submit" name="action" value="初始化数据"></p>

		</form>

	</div>

<?php
}

//添加菜单
function bzg_attachment_cdn_menu()
{
	if (function_exists('add_options_page')) {
		add_options_page('WordPress附件镜像存储', '附件云存储', 'administrator', 'attachment-cdn', 'bzg_attachment_cdn_control');
	}
}
add_action('admin_menu', 'bzg_attachment_cdn_menu');

//设置链接
function bzg_attachment_cdn_settings_link($action_links, $plugin_file)
{
	if ($plugin_file == plugin_basename(__FILE__)) {
		$settings_link = '<a href="options-general.php?page=attachment-cdn">设置</a>';
		array_unshift($action_links, $settings_link);
	}
	return $action_links;
}
add_filter('plugin_action_links', 'bzg_attachment_cdn_settings_link', 10, 2);
?>